import java.io.*;

public class CopyHomework {
    public static void main(String[] args) {
        File sDir = new File("E:\\ShippedExtensions");
        File tDir = new File("C:\\");
        //System.out.println(tDir.exists());
        copy(sDir,tDir);
    }

    private static void copy(File sDir, File tDir) {
        if(sDir.isFile()) {

            return;
        }

        File [] files = sDir.listFiles();
       // System.out.println(files.length);
        for(File file : files) {
            String f1 = file.getAbsolutePath().replace('E','C');
            if (file.isDirectory()) {
                copy(file,tDir);//如果是目录递归
                //System.out.println(file.getAbsoluteFile());

               // System.out.println(f1.replace('E','C'));
                //创建目录
                File file1 = new File(f1);
                file1.mkdirs();
                System.out.println(f1 + "创建成功");
            }else {//不是目录而是文件,则开始复制
                FileInputStream in = null;
                FileOutputStream out = null;
                try {
                    in = new FileInputStream(file);
                    out = new FileOutputStream(f1);
                    byte arr [] = new byte[1024 * 1024];
                    int inCount = 0;
                    while ((inCount = in.read(arr)) != -1 ) {
                        out.write(arr,0,inCount);
                        System.out.println(f1+"文件复制成功");
                    }
                    out.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
